package com.appwoo.txtws.component;

import android.content.Context;
import android.content.res.Resources;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class RootHelper
{
  private static String strRebooterPath = "";

  private static boolean GetIsRoot()
  {
    try
    {
      Process localProcess = Runtime.getRuntime().exec("su");
      DataOutputStream localDataOutputStream = new DataOutputStream(localProcess.getOutputStream());
      localDataOutputStream.writeBytes("chmod 755 " + strRebooterPath + "\n");
      localDataOutputStream.writeBytes("exit\n");
      localDataOutputStream.flush();
      localDataOutputStream.close();
      localProcess.waitFor();
      String str = new DataInputStream(localProcess.getErrorStream()).readLine();
      localProcess.destroy();
      if ((str != null) && (!str.contains("not allowed to su")))
      {
        boolean bool = str.contains("denied");
        if (bool)
          break label118;
      }
      label118: 
      while (true)
        return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean HasRoot(Context paramContext)
  {
    strRebooterPath = paramContext.getDir("raw", 2).getAbsolutePath() + "/rebooter";
    try
    {
      File localFile = new File(strRebooterPath);
      if ((!localFile.isFile()) || (localFile.length() < 100L))
      {
        InputStream localInputStream = paramContext.getResources().openRawResource(2131099649);
        byte[] arrayOfByte = new byte[localInputStream.available()];
        localInputStream.read(arrayOfByte);
        FileOutputStream localFileOutputStream = new FileOutputStream(strRebooterPath);
        localFileOutputStream.write(arrayOfByte);
        localFileOutputStream.close();
        localInputStream.close();
      }
      boolean bool = GetIsRoot();
      return bool;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  // ERROR //
  public static void RootCmd(String paramString)
  {
    // Byte code:
    //   0: invokestatic 25	java/lang/Runtime:getRuntime	()Ljava/lang/Runtime;
    //   3: ldc 27
    //   5: invokevirtual 31	java/lang/Runtime:exec	(Ljava/lang/String;)Ljava/lang/Process;
    //   8: astore_2
    //   9: new 33	java/io/DataOutputStream
    //   12: dup
    //   13: aload_2
    //   14: invokevirtual 39	java/lang/Process:getOutputStream	()Ljava/io/OutputStream;
    //   17: invokespecial 42	java/io/DataOutputStream:<init>	(Ljava/io/OutputStream;)V
    //   20: astore_3
    //   21: aload_3
    //   22: new 44	java/lang/StringBuilder
    //   25: dup
    //   26: aload_0
    //   27: invokestatic 118	java/lang/String:valueOf	(Ljava/lang/Object;)Ljava/lang/String;
    //   30: invokespecial 49	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   33: ldc 55
    //   35: invokevirtual 53	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   38: invokevirtual 59	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   41: invokevirtual 62	java/io/DataOutputStream:writeBytes	(Ljava/lang/String;)V
    //   44: aload_3
    //   45: ldc 64
    //   47: invokevirtual 62	java/io/DataOutputStream:writeBytes	(Ljava/lang/String;)V
    //   50: aload_3
    //   51: invokevirtual 67	java/io/DataOutputStream:flush	()V
    //   54: aload_3
    //   55: invokevirtual 70	java/io/DataOutputStream:close	()V
    //   58: aload_2
    //   59: invokevirtual 74	java/lang/Process:waitFor	()I
    //   62: pop
    //   63: aload_2
    //   64: invokevirtual 89	java/lang/Process:destroy	()V
    //   67: return
    //   68: astore_1
    //   69: aload_1
    //   70: invokevirtual 165	java/lang/Exception:printStackTrace	()V
    //   73: return
    //   74: astore_1
    //   75: goto -6 -> 69
    //
    // Exception table:
    //   from	to	target	type
    //   0	21	68	java/lang/Exception
    //   21	67	74	java/lang/Exception
  }

  public static void RootCmd2(String paramString, Context paramContext)
  {
    try
    {
      strRebooterPath = paramContext.getDir("raw", 2).getAbsolutePath() + "/rebooter";
      Process localProcess = Runtime.getRuntime().exec("su");
      DataOutputStream localDataOutputStream = new DataOutputStream(localProcess.getOutputStream());
      localDataOutputStream.writeBytes(strRebooterPath + paramString + "\n");
      localDataOutputStream.writeBytes("exit\n");
      localDataOutputStream.flush();
      localDataOutputStream.close();
      localProcess.waitFor();
      localProcess.destroy();
      return;
    }
    catch (Exception localException)
    {
    }
  }

  public static boolean hasRootAccess(Context paramContext)
  {
    strRebooterPath = paramContext.getDir("raw", 2).getAbsolutePath() + "/rebooter";
    try
    {
      File localFile = new File(strRebooterPath);
      if ((!localFile.isFile()) || (localFile.length() < 100L))
      {
        InputStream localInputStream = paramContext.getResources().openRawResource(2131099649);
        byte[] arrayOfByte = new byte[localInputStream.available()];
        localInputStream.read(arrayOfByte);
        FileOutputStream localFileOutputStream = new FileOutputStream(strRebooterPath);
        localFileOutputStream.write(arrayOfByte);
        localFileOutputStream.close();
        localInputStream.close();
      }
      boolean bool = GetIsRoot();
      return bool;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static void rebootDevice(Context paramContext)
  {
    RootCmd2(" -reboot", paramContext);
  }

  public static int runScriptAsRoot(Context paramContext, String paramString, StringBuilder paramStringBuilder)
  {
    ScriptRunner localScriptRunner = new ScriptRunner(new File(paramContext.getCacheDir(), "secopt.sh"), paramString, paramStringBuilder);
    localScriptRunner.start();
    try
    {
      localScriptRunner.join(40000L);
      if (localScriptRunner.isAlive())
      {
        localScriptRunner.interrupt();
        localScriptRunner.join(150L);
        localScriptRunner.destroy();
        localScriptRunner.join(50L);
      }
      label63: return localScriptRunner.exitcode;
    }
    catch (InterruptedException localInterruptedException)
    {
      break label63;
    }
  }

  public static void shutDown(Context paramContext)
  {
    RootCmd2("", paramContext);
  }

  private static final class ScriptRunner extends Thread
  {
    private Process exec;
    public int exitcode = -1;
    private final File file;
    private final StringBuilder res;
    private final String script;

    public ScriptRunner(File paramFile, String paramString, StringBuilder paramStringBuilder)
    {
      this.file = paramFile;
      this.script = paramString;
      this.res = paramStringBuilder;
    }

    public void destroy()
    {
      try
      {
        if (this.exec != null)
          this.exec.destroy();
        this.exec = null;
        return;
      }
      finally
      {
      }
    }

    public void run()
    {
      try
      {
        this.file.createNewFile();
        String str = this.file.getAbsolutePath();
        Runtime.getRuntime().exec("chmod 777 " + str).waitFor();
        OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(new FileOutputStream(this.file));
        if (new File("/system/bin/sh").exists())
          localOutputStreamWriter.write("#!/system/bin/sh\n");
        localOutputStreamWriter.write(this.script);
        if (!this.script.endsWith("\n"))
          localOutputStreamWriter.write("\n");
        localOutputStreamWriter.write("exit\n");
        localOutputStreamWriter.flush();
        localOutputStreamWriter.close();
        this.exec = Runtime.getRuntime().exec("su");
        DataOutputStream localDataOutputStream = new DataOutputStream(this.exec.getOutputStream());
        localDataOutputStream.writeBytes(str);
        localDataOutputStream.flush();
        localDataOutputStream.close();
        InputStreamReader localInputStreamReader1 = new InputStreamReader(this.exec.getInputStream());
        arrayOfChar = new char[1024];
        while (true)
        {
          int i = localInputStreamReader1.read(arrayOfChar);
          if (i == -1)
          {
            InputStreamReader localInputStreamReader2 = new InputStreamReader(this.exec.getErrorStream());
            j = localInputStreamReader2.read(arrayOfChar);
            if (j != -1)
              break;
            if (this.exec != null)
              this.exitcode = this.exec.waitFor();
            return;
          }
          if (this.res != null)
            this.res.append(arrayOfChar, 0, i);
        }
      }
      catch (InterruptedException localInterruptedException)
      {
        while (true)
        {
          char[] arrayOfChar;
          int j;
          if (this.res != null)
            this.res.append("\nOperation timed-out");
          return;
          if (this.res != null)
            this.res.append(arrayOfChar, 0, j);
        }
      }
      catch (Exception localException)
      {
        if (this.res != null)
          this.res.append("\n" + localException);
        return;
      }
      finally
      {
        destroy();
      }
    }
  }
}

/* Location:           C:\Documents and Settings\Administrator\classes_dex2jar.jar
 * Qualified Name:     com.appwoo.txtws.component.RootHelper
 * JD-Core Version:    0.6.2
 */